import sys
# import resource
from learning.StatePrefixEqOracleFailSafe import StatePrefixOracleFailSafe
from learning.FailSafeCacheSUL import FailSafeCacheSUL, print_error_info
from aalpy.learning_algs import run_Lstar
from aalpy.utils import visualize_automaton
from pesp4.IKESUL import *
import shutil
args_len = len(sys.argv) - 1

if args_len < 5:
    sys.exit("Too few arguments provided.\nUsage: python3 ipsec_learning.py 'version' 'local_ip' 'remote_ip' 'iface' 'object' 'out_dir'")
# python ipsec_query.py v2 192.168.38.1 192.168.38.134 'VMware Virtual Ethernet Adapter for VMnet8' strongswan IKEv2_query_strongswan
IKE_version = sys.argv[1]
local_ip = sys.argv[2]
remote_ip = sys.argv[3]
iface = sys.argv[4]
object = sys.argv[5]
out_dir = sys.argv[6]

if os.path.exists(out_dir):
    shutil.rmtree(out_dir)
os.makedirs(out_dir)

learned_model_name = f'{out_dir}/learned_model_strongswan'

ike_sul = IKESUL(local_ip, remote_ip, iface=iface, version=IKE_version, object=object, dir=out_dir)

if IKE_version == 'v1':
    alphabet = ['main_mode_1', 'main_mode_2', 'main_mode_3', 'quick_mode_1',  'quick_mode_2', 'test_ESP', 'delete_ESP', 'delete_IKE']
else:
    # alphabet = ['SAINIT_SA-KE-NONCE', 'AUTH_IDi-AUTH-SA-TSi-TSr', 'CHILDSA_SA-NONCE-TSi-TSr', 'CHILDSA_RekeyIKE-KE-NONCE', 'OI_CHILDSA_SA-NONCE-TSi-TSr', 'OI_INFO_DelIKE', 'CHILDSA_RekeySA-SA-NONCE-TSi-TSr', 'test_ipsec', 'test_old_ipsec', 'INFO_DelOldChild', 'INFO_DelChild', 'INFO_', 'INFO_DelIKE']
    alphabet = ['SAINIT_SA-KE-NONCE', 'AUTH_IDi-AUTH-SA-TSi-TSr', 'CHILDSA_SA-NONCE-TSi-TSr', 'CHILDSA_RekeySA-SA-NONCE-TSi-TSr', 'test_ipsec', 'INFO_DelIKE']
    # alphabet = ['SAINIT_SA-KE-NONCE', 'AUTH_IDi-AUTH-SA-TSi-TSr']

# alphabet = ['AUTH_IDi-AUTH-SA-TSi-TSr', 'SAINIT_SA-KE-NONCE']

ike_sul.query(alphabet)
